package com.vincce.sport.entity;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页对象
 * User: Henry Young
 * Date: 2014-03-11
 * Time: 11:25:40 AM
 * To change this template use File | Settings | File Templates.
 */
public class Page<T> {  
	/**
	 * 默认每页记录数
	 */
	@JSONField(serialize = false)
	private static int DEFAULT_LIMIT = 10;



	/**
	 * 取数据偏移
	 * Rest Api使用
	 */
	private long offset = 0;

	/**
	 * 总数据量
	 */
	private long total = 0;

	/**
	 * 取数据数量
	 * Rest Api使用
	 */
	private int limit = DEFAULT_LIMIT;

	/**
	 * 查询数据结果
	 */
	private List<T> result = new ArrayList<T>();

	/**
	 * 查询条件存放处
	 */
	private Map<String, Object> filter = new HashMap<String, Object>();


    @JSONField(serialize = false)
	public long getPage() {
		return (offset / limit) + 1;
	}

	public void setPage(long page) {
		offset = (page - 1) * limit;
	}

	public void setPage(Long page) {
		setPage(page != null ? page.longValue() : 1);
	}

	@JSONField(serialize = false)
	public long getTotalPage() {
		if (total < 0) {
			return -1;
		}
		long count = total / limit;
		if (total % limit != 0) {
			count++;
		}
		return count;
	}

	public long getOffset() {
		return offset;
	}

	public void setOffset(long offset) {
		this.offset = offset;
	}

	public void setOffset(Long offset) {
		setOffset(offset != null ? offset.longValue() : 0);
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		if (limit <= 0) {
			limit = DEFAULT_LIMIT;
		}
		this.limit = limit;
	}

	public void setLimit(Integer limit) {
		setLimit(limit != null ? limit.intValue() : DEFAULT_LIMIT);
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public Map<String, Object> getFilter() {
		return filter;
	}

	public void setFilter(Map<String, Object> filter) {
		this.filter = filter;
	}


    private String keyWords;

    public void setKeyWords(String keyWords){
        this.keyWords=keyWords;
    }
    public String getKeyWords() {
        return keyWords;
    }
}
